<?php

require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."Categorys.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."Tags.php";

require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."TagItemAssocs.php";
require_once ROOT_LIBRARY_UTILITY.DS."Converter.php";

class Application_Forms_Article extends Zend_Form
{
    public function init()
    {
        $this->setName('article');

        /* Hiden field, some field use in future extend */
        
        $article_id = new Zend_Form_Element_Hidden('article_id');
        $status = new Zend_Form_Element_Hidden('status');
        $num_views = new Zend_Form_Element_Hidden('num_views');
        $created_date = new Zend_Form_Element_Hidden('created_date');
        $created_user_id = new Zend_Form_Element_Hidden('created_user_id');
        $created_user_name = new Zend_Form_Element_Hidden('created_user_name');
        $updated_date = new Zend_Form_Element_Hidden('updated_date');
        $updated_user_id = new Zend_Form_Element_Hidden('updated_user_id');
        $updated_user_name = new Zend_Form_Element_Hidden('updated_user_name');
        $activate_user_id = new Zend_Form_Element_Hidden('activate_user_id');
        $activate_user_name = new Zend_Form_Element_Hidden('activate_user_name');
        $activate_date = new Zend_Form_Element_Hidden('activate_date');
        $allow_comment = new Zend_Form_Element_Hidden('allow_comment');
        $num_comments = new Zend_Form_Element_Hidden('num_comments');
        $show_date = new Zend_Form_Element_Hidden('show_date');
        
        /* Visible field */
        
	$language = new Zend_Form_Element_Select('language');
        $language->setLabel('Ngôn ngữ:');
        $language->addMultiOptions(array(
                        ''=>'',
                 	'en' => 'English',
                	'vi' => 'VietNam'
        ));
        
        $source = new Zend_Form_Element_Text('source');
        $source->setLabel('Nguồn');

        $title = new Zend_Form_Element_Text('title');
        $title->setLabel('Tên bài viết:')
              ->setRequired(true)
              ->addFilter('StripTags')
              ->addValidator('NotEmpty');

        $sub_title = new Zend_Form_Element_Text('sub_title');
        $sub_title->setLabel('Tên khác:')
              ->setRequired(false)
              ->addFilter('StripTags');
             
        $description = new Zend_Form_Element_Textarea('description');
        $description->setLabel('Mô tả:')
               ->setRequired(true)
               ->addFilter('StripTags')
               ->addValidator('NotEmpty');
			   
        $content = new Zend_Form_Element_Textarea('content');
        $content->setLabel('Nội dung:')
               ->setRequired(true)
               ->addFilter('StripTags')
               ->addValidator('NotEmpty');			   
               
        $author = new Zend_Form_Element_Text('author');
        $author->setLabel('Tác giả:')
              ->setRequired(true)
              ->addFilter('StripTags');               
               
               
        $allow_comment = new Zend_Form_Element_Checkbox('allow_comment');
		$allow_comment->setLabel('Cho phép phản hồi ?');
		
        $hot_article = new Zend_Form_Element_Checkbox('is_hot');
		$hot_article->setLabel('Bài viết gây chú ý ?');

        $sticky = new Zend_Form_Element_Checkbox('sticky');
		$sticky->setLabel('Đặt bài viết lên vị trí đầu ?');	

        $category = new Zend_Form_Element_Select('category_id');
        $category->setLabel('Chuyên mục:');	

        $tag = new Zend_Form_Element_MultiCheckbox('tag');
        //$tag->setLabel('Tags');	
		
		/* Submit button */
	   
        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setAttrib('id', 'submitbutton');
		
		$preview = new Zend_Form_Element_Submit('Preview');
        $preview->setAttrib('id', 'previewbutton');
        
        $revision = new Zend_Form_Element_Submit('Revision');
        $revision->setAttrib('id', 'revisionButton');
		
		/* Load form data */
		
		/* Language */
		
		/* Category */
        
        $category_model = new End_Models_Categorys();
        $categories = $category_model->fetchAll();
        foreach($categories as $c) {
        	$category->addMultiOption($c->category_id,
        		$c->name);
        }
		
		/* Tag */
        
        $tag_model = new Application_Models_Tags();
        $tags = $tag_model->fetchAll();
        foreach ($tags as $t) {
        	$tag->addMultiOption($t->tag_id,
        		$t->tag_text);
        }

        $this->addElements(array($title, $sub_title, $author, $language, $source, $category, 
			$description, $content, 
			$allow_comment, $hot_article, $sticky, $tag,
			$submit, $preview, $revision,
			$article_id, $status, $num_views, $created_date,
			$created_user_id, $created_user_name, $updated_date,
			$updated_user_id, $updated_user_name, $activate_user_id,
			$activate_user_name, $activate_date, $allow_comment, $num_comments,
			$show_date));
    }
    
    
    /**
     * Override populate function, add tags to value
     * @see Zend_Form::populate()
     */
    public function populate($values) {
    	/* Get all tag assoc with current article */
    	$tag_item_assoc_model = new Application_Model_Tag_Item_Assocs();
    	$tag_objects = $tag_item_assoc_model->FindTags($values["article_id"]);
    	$tag_ids = array();
    	$i = 0; //counter
    	foreach ($tag_objects as $t) {
    		// $temp is array, $t is object
    		$temp = Utility_Converter::objectToArray($t);
    		$tag_ids[$i] = $temp['tag_id'];
    		$i++;
    	}
		$values["tag"] = $tag_ids;
    	parent::populate($values);
    }
}